<template>
  <el-dialog :append-to-body="true" :inline="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="630px">
    <order-from
      ref="orderfrom"
      v-show="isAdd?from:false"
      v-model="orderParam"
      :is-add="isAdd"
      @cancel="cancel"
      @nextStep="nextStep">
    </order-from>
    <order-info
      ref="orderinfo"
      v-show="isAdd?info:true"
      v-model="sourceInfo"
      :is-add="isAdd"
      :source-from="sourceFrom"
      @cancel="cancel"
      @doSubmit="doSubmit"
      @nextStep="nextStep"
      @prevStep="prevStep">
    </order-info>
  </el-dialog>
</template>
<script>
import request from '@/utils/request'
import OrderFrom from './components/orderFrom';
import OrderInfo from './components/orderInfo';
import { add, edit } from '@/api/tWorkOrder'
export default {
  components: {OrderFrom, OrderInfo},
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      from:true,
      info:false,
      sourceFrom:"1",
      sourceInfo:{
        accident:{
          id: '',
          customerId: '',
          customerName: '',
          faultDevice: '',
          faultDesc: '',
          faultPicture: '',
          reqiureTime: '',
          address: '',
          notes: '',
          linker: '',
          cellphone: '',
          inputOperator: '',
          inputTime: '',
          source: '3',
          fileList:[],
          faultDevices:[]
        },
        task:{
          taskTitle: '',
          taskContent: '',
          attachments: '',
          arriveTime: '',
          finishTime: '',
          notes: '',
          linker: '',
          cellphone: '',
          inputOperator: '',
          inputTime: '',
          fileList:[]
        },
        inspection:{
          planName: '',
          objId: '',
          objName: '',
          templateId: '',
          templateName: '',
          planDesc: '',
          periodId: '',
          periodName: '',
          inspectorId: '',
          inspectorName: '',
          vehTypeId: '',
          vehTypeName: '',
          startTime: '',
          endTime: '',
          operateTime: '',
          state: ''
        },
        orderParam: {
          id: '',
          orderCode: '',
          orderSource: '',
          sourceId: '',
          status: '',
          serviceLevel: '',
          contractId: '',
          faultType: '',
          supplier: '',
          addriveTime: '',
          serviceTime: '',
          requireTool: '',
          notes: '',
          assignOperator: '',
          assignTime: '',
          acceptOperator: '',
          acceptTime: '',
          engineer: ''
        }
      },
      orderParam: {
        id: '',
        orderCode: '',
        orderSource: '',
        sourceId: '',
        status: '100',
        serviceLevel: '',
        contractId: '',
        faultType: '',
        supplier: '',
        addriveTime: '',
        serviceTime: '',
        requireTool: '',
        notes: '',
        assignOperator: '',
        assignTime: '',
        acceptOperator: '',
        acceptTime: '',
        engineer: ''
      },
    }
  },
  methods: {
    prevStep() {
      this.from=true
      this.info = false
    },
    nextStep(sourceId,orderSource,orderParam) {
      this.sourceInfo.orderParam = orderParam;
      this.sourceFrom = orderSource;
      const params = {
        id:sourceId
      }
      if(orderSource=="1"){
        request({
          url: 'api/tAccident',
          method: 'get',
          params
        }).then(res => {
          const content = [].concat(res.content);
          const _this = this
          content.forEach(function(data, index) {
            if(data.id==sourceId){
              _this.sourceInfo.accident = {
                id: data.id,
                customerId:data.customerId,
                customerName: data.customerName,
                faultDevice: data.faultDevice,
                faultDesc: data.faultDesc,
                faultPicture: data.faultPicture,
                reqiureTime: new Date(data.reqiureTime),
                address: data.address,
                notes: data.notes,
                linker: data.linker,
                cellphone: data.cellphone,
                inputOperator: data.inputOperator,
                inputTime: new Date(data.inputTime),
                source: data.source,
                faultDevices:[]
              }
              const pics = [];
              data.faultPicture.split(',').forEach(function(data1, index1) {
                if(data1!=null && data1!=''){
                  pics.push({url:'http://localhost:8000/api/picture/'+data1})
                }
              })
              _this.sourceInfo.accident.fileList = [].concat(pics);

              data.faultDevice.split(',').forEach(function(data, index) {
                _this.sourceInfo.accident.faultDevices.push(Number(data))
              })
            }
          })
          this.from=false
          this.info = true
        }).catch(err => {
          console.log(err.response.data.message)
        })  
      }
      if(orderSource=="2"){
        request({
          url: 'api/tTasks',
          method: 'get',
          params
        }).then(res => {
          const content = [].concat(res.content);
          const _this = this
          content.forEach(function(data, index) {
            if(data.id==sourceId){
              _this.sourceInfo.task = {
                id: data.id,
                taskTitle: data.taskTitle,
                taskContent: data.taskContent,
                attachments: data.attachments,
                arriveTime: new Date(data.arriveTime),
                finishTime: new Date(data.finishTime),
                notes: data.notes,
                linker: data.linker,
                cellphone: data.cellphone,
                inputOperator: data.inputOperator,
                inputTime: new Date(data.inputTime)
              }
              const pics = [];
              data.attachments.split(',').forEach(function(data1, index1) {
                if(data1!=null && data1!=''){
                  pics.push({url:'http://localhost:8000/api/picture/'+data1,id:data1})
                }
              })
              _this.sourceInfo.task.fileList = [].concat(pics);
            }
          })
          this.from=false
          this.info = true
        }).catch(err => {
          console.log(err.response.data.message)
        }) 
      }
      if(orderSource=="3"){
        request({
          url: 'api/tInspectionTask',
          method: 'get',
          params
        }).then(res => {
          const content = [].concat(res.content);
          const _this = this
          content.forEach(function(data, index) {
            if(data.id==sourceId){
              _this.sourceInfo.inspection = {
                id: data.id,
                planName: data.planName,
                objName: data.objName,
                templateName: data.templateName,
                planDesc: data.planDesc,
                periodName: data.periodName,
                inspectorName: data.inspectorName,
                vehTypeName: data.vehTypeName
              }
            }
          })
          this.from=false
          this.info = true
        }).catch(err => {
          console.log(err.response.data.message)
        }) 
      }
      

    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.orderParam).then(res => {
        this.cancel()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        //this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        //this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.orderParam).then(res => {
        this.cancel()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        //this.loading = false
        this.sup_this.init()
      }).catch(err => {
        //this.loading = false
        console.log(err.response.data.message)
      })
    },
    cancel() {
      this.from=true
      this.info = false
      this.dialog = false
      this.orderParam= {
        id: '',
        orderCode: '',
        orderSource: '',
        sourceId: '',
        status: '',
        serviceLevel: '',
        contractId: '',
        faultType: '',
        supplier: '',
        addriveTime: '',
        serviceTime: '',
        requireTool: '',
        notes: '',
        assignOperator: '',
        assignTime: '',
        acceptOperator: '',
        acceptTime: '',
        engineer: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
